package cn.edu.nuc.ssm.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.ssm.domain.Comment;
import cn.edu.nuc.ssm.domain.Film;
import cn.edu.nuc.ssm.domain.Sysfunction;
import cn.edu.nuc.ssm.domain.Sysuser;
import cn.edu.nuc.ssm.service.interfaces.ContentService;
import cn.edu.nuc.ssm.service.interfaces.FilmService;
import cn.edu.nuc.ssm.service.interfaces.SysRoleService;
import cn.edu.nuc.ssm.service.interfaces.SysUserService;

@Controller
public class LoginController {
    @Autowired
	private SysUserService service;
    @Autowired
    private SysRoleService roleservice;
	@Autowired
	private FilmService filmServiceImpl;
	@Autowired
	private ContentService cservice;
    
    //进入后台登录页面
    @RequestMapping(value="login" , method=RequestMethod.GET)
    public String login(){
    	return "sys/user/login";
    }
    //用户登录
    @RequestMapping(value="login" , method=RequestMethod.POST)
    public String login(String username ,String password,HttpSession session,Model model){
    	Sysuser user = new Sysuser();
    	user.setUsername(username);
    	user.setPassword(password);
    	List<Sysuser> list = service.login(username,password);
    	if(list.size() > 0){
    		if(list.get(0).getUserstate().intValue() == 1){
    			session.setAttribute("user", list.get(0));
	    		int roleid = list.get(0).getRoleid();
    			//后台登录,用户登录跳转到前台首页。
    			if(!list.get(0).getRolename().equals("用户")){
		    		//通过roleid查询角色功能,保存到model传到页面。
		    		List<Sysfunction> list2 = roleservice.initfunction2(roleid);
		    		System.out.println("得到功能列表"+ list2.size()+list2);
		    		model.addAttribute("list", list2);
		    		return "sys/index2";
	    		}else{
	    			return "redirect:/index";
	    		}
    		}else{
    	    	model.addAttribute("msg", "用户已经被锁定，请联系管理员！");
    	    	return "sys/user/login";
    		}	
    	}
    	model.addAttribute("msg", "用户名或者密码错误，请重新登录！");
    	return "sys/user/login";
    }
    
  //注销用户
  	@RequestMapping(value="logout",method=RequestMethod.GET)
  	public String logout(HttpServletRequest request,HttpSession session){
  		session = request.getSession();
  		Sysuser user = (Sysuser) session.getAttribute("user");
  		if(user!=null && user.getUsername()!=null){
  			session.removeAttribute("user");
  			if(!"用户".equals(user.getRolename())){   //后台注销跳到登录
  				return "/sys/user/login";
  			}else{
  				return "redirect:/index";          //用户在前台注销跳到主页。
  			}
  		}
  		return null;
  	}
  	
    
    //注册 Ajax 判断用户名是否已经注册
    @RequestMapping(value="ajax/regist",method=RequestMethod.POST)
    public void regist(String username,HttpServletResponse response){
    	List<String> usernames = service.getUsernames();
    	String result = null;
    //返回一个html给Ajax
    	    if(usernames.contains(username)){
        		result = "<font color='red'>该用户名已经注册</font>";
        	}else{
        		result = "<font color='green'>该用户名可以使用</font>";
        	}
    	
    	//返回一个   html  
    	try {
    		response.getWriter().print(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    //前台用户注册成功跳到前台登录界面
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String adduser(Sysuser user,Model model){
		   int result = service.regist(user);
		   if(result == 1){
			  return "forward:login.jsp";
		   }
		return "forward:regist.jsp";
	}
	
	// 前台获取电影信息
	@RequestMapping(value = "detail2/{filmid}", method = RequestMethod.GET)
	public String updateFilm2(@PathVariable("filmid") Integer filmid, Model model) {
		Film film = filmServiceImpl.selectByFilmid(filmid);
		System.out.println(film.getTicket());
		model.addAttribute("film", film);
		return "forward:/detail.jsp";
	}
	
	//前台发表评论时。（发表成功返回电影详细信息。并显示Ajax显示评论）
	@RequestMapping(value="add3",method=RequestMethod.POST)
	public String fabiao(Comment type,HttpServletRequest req){
		HttpSession session = req.getSession();
		Sysuser user = (Sysuser) session.getAttribute("user");
		if(user != null && user.getUsername()!=null){
			
			int result = cservice.insert(type);
	    	if(result == 1){
	    	  return "redirect:/detail2/"+type.getFilmId();
	    	}
		}else{
			return "forward:login.jsp";
		}
		return "error";
	}
}
